<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/8/18
 * Time: 16:54
 */

namespace app\admin\controller;


use think\Db;
use think\Request;
use app\admin\model\ClassroomType as ClassroomTypeModel;//课程类型
use app\admin\model\Classroom as ClassroomModel;//课程

class System extends Base
{
    /**
     * 课程分类列表
     * @return mixed|\think\response\Json
     */
    public function classroomTypeList(){
        $classroomTypeModel = new ClassroomTypeModel();
        $count = $classroomTypeModel->getCount();
        if(Request::instance()->isAjax()){
            $page = input('page') ? input('page') : 1;
            $limits = input('limit') ? input('limit') : 10;// 获取总条数;
            $lists = Db::table('up_classroom_type')->alias('uct')
                ->join('up_admin ua1', 'ua1.id = uct.create_admin', 'left')
                ->join('up_admin ua2', 'ua2.id = uct.update_admin', 'left')
                ->field('classroom_id, classroom_name, uct.create_time, uct.update_time, 
            ua1.real_name as create_name, ua2.real_name as update_name')
                ->page($page, $limits)
                ->order('classroom_id desc')
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $this->assign([
            'count' => $count
        ]);
        return $this->fetch('system/classroomtypelist');
    }
    
    /**
     * 新建课程类型
     * @return array|mixed
     */
    public function classroomTypeAdd(){
        $classroomTypeModel = new ClassroomTypeModel();
        if(Request::instance()->isAjax()){
            $classroom_name = trim(input('classroom_name'), ' ');
            $res = $classroomTypeModel->sameName($classroom_name);
            if($res){
                return ['code' => 100, 'data' => '', 'msg' => '分类名称已被使用'];
            }
            $data = [
                'classroom_name' => $classroom_name,
                'create_admin' => session('uid'),//创建者
                'create_time' => date('Y-m-d H:i:s'),//创建时间
                'update_admin' => session('uid'),//编辑者
                'update_time' => date('Y-m-d H:i:s'),//最后编辑时间
            ];
            $result = $classroomTypeModel->insertClassroomType($data);
            if($result){
                return ['code' => 200, 'data' =>"", 'msg' => '新建课程类型成功'];
            }else{
                return ['code' => 100, 'data' => '', 'msg' => '新建课程类型成功失败'];
            }
        }
        return $this->fetch('system/classroomtypeadd');
    }
    
    /**
     * 更新分类名称
     * @return array|mixed
     */
    public function classroomTypeEdit(){
        $classroomTypeModel = new ClassroomTypeModel();
        if(Request::instance()->isAjax()){
            $classroom_name = trim(input('classroom_name'), ' ');
            $res = $classroomTypeModel->sameName($classroom_name);
            if($res){
                return ['code' => 100, 'data' => '', 'msg' => '分类名称已被使用'];
            }
            $data = [
                'classroom_name' => $classroom_name,
                'update_admin' => session('uid'),//编辑者
                'update_time' => date('Y-m-d H:i:s'),//最后编辑时间
            ];
            $result = $classroomTypeModel->updateClassroomType(input('classroom_id'), $data);
            if($result){
                return ['code' => 200, 'data' =>"", 'msg' => '更新成功'];
            }else{
                return ['code' => 100, 'data' => '', 'msg' => '更新失败'];
            }
        }
        $classroom_id = input('id');
        $classroom_name = $classroomTypeModel->getTypeName($classroom_id);
        $this->assign([
            'classroom_name' => $classroom_name,
            'classroom_id' => $classroom_id
        ]);
        return $this->fetch('system/classroomtypeedit');
    }
    
    /**
     * 删除课程类型
     * @return array
     */
    public function classroomTypeDel(){
        $classroom_id = input('ids');
        $classroomModel = new ClassroomModel();
        $classroomTypeModel  = new ClassroomTypeModel();
        $where = [];
        $where['classroom_id'] = ['=', $classroom_id];
        $info = $classroomModel->getInfo($where);
        if($info){
            return ['code' => 100, 'data' => '', 'msg' => '该类型已被占用，不允许删除'];
        }
        $res = $classroomTypeModel->delClassroomType($classroom_id);
        if($res){
            return ['code' => 200, 'data' =>"", 'msg' => '删除成功'];
        }else{
            return ['code' => 100, 'data' => '', 'msg' => '删除失败'];
        }
    }
}